package wc

import (
	"errors"
	"fmt"
	"io/ioutil"
	"main/env"
	"net/http"
	"net/url"
)

type RESP struct {
	SID    string `json:"sid"`
	Status int    `json:"status"`
}

func RunIDServer(uid string) (string, error) {
	u := url.Values{}
	u.Add("uid", uid)
	resp, err := http.Get(fmt.Sprintf("http://%s/get_sid?%s", env.IDServer, u.Encode()))
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	var res RESP
	err = res.UnmarshalJSON(content)
	if err != nil {
		return "", err
	}

	if res.Status == 0 {
		return "", errors.New("user auth throw a problem")
	}
	return res.SID, nil
}
